class AcceptedController < ApplicationController
  before_filter :require_any_user
  skip_before_filter :verify_authenticity_token, :only=>[:up,:show_all]

  def index
    @wts=WeekTimeSheet.find :all, :conditions=>'submit=1', :order=> 'start_date  ASC '
    arr=[  ]
    @wts.each{ |current_wts|
      if current_wts.task.project.user_id==current_user.id || current_wts.task.project.id==1
        
        #I add the owner user for the Task
        user=current_wts.task.user.complete_name
        user_id=current_wts.task.user.id
        currStartDate=current_wts.start_date.to_date
        
        if current_wts.active==0 && current_wts.submit==1
                  status='<font color=red>Por revisar</font>'
        end
        if current_wts.active==1
                    status='<font color=green>Aceptado</font>'
        end

        arr << [currStartDate, user,user_id,status]
        currStartDate+=7

       ##########################################
      end
    }
    @dates=arr.uniq
  end

  def submit
    #ToDo: validate the owner of the project :) It ought let vulns
    ids=params['id'].split(",")
    ids.each{ |i|
      i=i.to_i
      if i > 0
        thisWTS=WeekTimeSheet.find(i)
        thisWTS.active='1'
        thisWTS.submit='1'
        thisWTS.save
      end
    }
  end

  def nosubmit
    #ToDo: validate the owner of the project :) It ought let vulns
    ids=params['id'].split(",")
    ids.each{ |i|
      i=i.to_i
      if i > 0
        thisWTS=WeekTimeSheet.find(i)
        thisWTS.active='0'
        thisWTS.submit='0'
        thisWTS.save
      end
    }
  end

  def edit
    #I find at least one projects whose start_date<=params[:myDate].(monday+6), in other words, if the task has been initialized, user_id=current_user  and It has not been completed, if so, I'll show the TimeSheet Grid.
    @projects=Project.find( :all, :conditions=>"user_id='"+ current_user.id.to_s+"'" )
    @monday=params[:myDate].to_date
    @user=User.find( params[:id].to_i )
    @projects.each {  |p|
      id_p=p.id
      @anyTask=Task.find(:all,:conditions=>"project_id='"+id_p.to_s+"' and start_date<='"+(@monday+6).to_s+"'")
      if @anyTask.size>0
        return 0
      end
    }
  end


  def up
    #It never gonna be a negative number
    #como característica cuando la tarea tenga el 100% de completado no se mostrará más
    a={:monday_time=>params['c3'],:tuesday_time=>params['c4'],:wednessday_time=> params['c5'],:thursday_time=> params['c6'],:friday_time=> params['c7'],:saturday_time=> params['c8'],:sunday_time=> params['c9']}
    @wts = WeekTimeSheet.find(params[:gr_id])

    if @wts.task.percentage!=params['c1'].to_i
      @wts.task.percentage=params['c1'].to_i
        if @wts.task.percentage==0
          @wts.task.status="Nuevo"
        else
          if (@wts.task.percentage>0 && @wts.task.percentage<100)
            @wts.task.status="En proceso"
          else
            @wts.task.status="Completado"
          end
        end
    else
        @wts.task.status=params['c2']
    end

    @wts.task.save
    @wts.update_attributes( a )

  end


  def show_all

    #---------- Dates and configuration
    response.headers["Content-Type"] = 'text/xml'
    now=Date.today.to_s
    @monday=Date.today.monday
    if params['start_date'].nil?
      sd=@monday
    else
      sd=(params['start_date']).to_date
    end
    @sd=sd
    #project must be started 'till this date, the owner of the projects must be current_user
    @projectsFromDB=Project.find :all, :conditions=>( "startDate <= '"+(sd+6).to_s+"' and user_id='"+current_user.id.to_s+"' or id=1" )
    @projs4ThisWeek=[]
    @projectsFromDB.each{ |project|
      id_p=project.id

      #The task must be asociated with the user_id recived by arguments. It should be parsed correctly
      tasksFromCondition=project.tasks.find :all, :conditions=> (" project_id='"+id_p.to_s+"' and user_id='"+params[:id].to_i.to_s+"'") 
      p=project.clone
      p.tasks=[]
      tasksFromCondition.each do |thisTask|
        if thisTask.start_date.to_date.monday <= sd+6
          p.tasks<<thisTask
        end
      end
      @projs4ThisWeek << p
    }


    @projs4ThisWeek.each do |p|
      p.tasks.each do |t|

        #----------- Ordering everything for XML response
       thisWTS=WeekTimeSheet.find(:all, :conditions => 'task_id='+t.id.to_s+" and start_date='"+sd.to_s+"'"+" and submit=1 ", :limit=>"1")
       if thisWTS.size > 0 #La hoja semanal fue mandada? 
          w= WeekTimeSheet.find(thisWTS[0].id)
       end

      end
    end

  end

  def next_date
    if params['id'].nil?
      sd=@monday=Date.today.monday
    else
      sd=(params['id']).to_date+7
    end
    @myDate=sd;
    @outHeader="Nombre,%,Estado,Lu("+(sd).to_s(:short)+"),Ma("+(sd+1).to_s(:short)+"),Mi("+(sd+2).to_s(:short)+"),Ju("+(sd+3).to_s(:short)+"),Vi("+(sd+4).to_s(:short)+"),Sa("+(sd+5).to_s(:short)+")"+",Do("+(sd+6).to_s(:short)+"),suma";
  end

  def preview_date
    if params['id'].nil?
      sd=@monday=Date.today.monday
    else
      sd=(params['id']).to_date-7
    end
    @myDate=sd;
    @outHeader="Nombre,%,Estado,Lu("+(sd).to_s(:short)+"),Ma("+(sd+1).to_s(:short)+"),Mi("+(sd+2).to_s(:short)+"),Ju("+(sd+3).to_s(:short)+"),Vi("+(sd+4).to_s(:short)+"),Sa("+(sd+5).to_s(:short)+")"+",Do("+(sd+6).to_s(:short)+"),suma";
  end



  def status

    @monday=Date.today.monday
    if params['start_date'].nil?
      sd=@monday
    else
      sd=(params['start_date']).to_date
    end
    @sd=sd

       thisWTS=WeekTimeSheet.find(:all, :conditions => "start_date='"+sd.to_s+"' and submit=1")
       @status="Por Revisar"
       thisWTS.each{ |w|
           if w.task.user_id==params[:id].to_i
              if  w.active==1
               @status="Aceptado"
              else
              end
           end
      }
      

  end



end
